package web;

import com.google.gson.Gson;
import pojo.Goods;
import service.GoodsService;
import service.impl.GoodsServiceImpl;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lq
 * @create 2021/1/9
 */
public class GoodsServlet extends BaseServlet {
    private GoodsService goodsService = new GoodsServiceImpl();

    /**
     * 添加商品
     * @param request goods(至少需要id,storeId,ownerId,num)
     * @param response 1表示添加成功，0表示添加失败
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Goods goods = WebUtils.copyParamToBean(request.getParameterMap(),new Goods());
        Map<String, Integer> map = new HashMap<>(16);
        if(goodsService.addGoods(goods)){
            map.put("status", 1);
        }else{
            map.put("status", 0);

        }
        Gson gson = new Gson();
        String goodsJson = gson.toJson(map);
        response.getWriter().print(goodsJson);
    }

    /**
     * 删除商品
     * @param request 商品的id
     * @param response 1为删除成功，0为删除失败
     */
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Map<String, Integer> map = new HashMap<>(16);
        if(goodsService.deleteGoods(id)){
            map.put("status", 1);
        }else{
            map.put("status", 0);
        }
        Gson gson = new Gson();
        String goodsJson = gson.toJson(map);
        response.getWriter().print(goodsJson);
    }

    /**
     * 出（入）库商品
     * @param request 商品id;num
     * @param response 1为出（入）库成功，0出（入）库失败
     */
    protected void port(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Integer num = Integer.parseInt(request.getParameter("num"));
        Map<String, Integer> map = new HashMap<>(16);
        if(goodsService.portGoods(id, num)){
            map.put("status",1);
        }else{
            map.put("status", 0);
        }
        Gson gson = new Gson();
        String goodsJson = gson.toJson(map);
        response.getWriter().print(goodsJson);
    }

    /**
     * 查找所有商品信息
     * @param request 无
     * @param response 商品列表 及 status:1表示成功获取商品，0表示失败
     */
    protected void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();
        List<Goods> goods = goodsService.queryAllGoods();
        String goodsJson =null;
        if(goods!=null){
            goodsJson = "{\"status\":1,\"info\":"+gson.toJson(goods)+"}";
        }else{
            goodsJson = "{\"status\":0}";
        }
        response.getWriter().print(goodsJson);
    }

    /**
     * 查询指定用户或者仓库的所有 商品
     * @param request id;chose 0则表示用户id，1则表示仓库id
     * @param response 商品列表 及 status:1表示成功获取商品，0表示失败
     */
    protected void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Integer chose= Integer.parseInt(request.getParameter("chose"));
        Gson gson = new Gson();
        List<Goods> goods = goodsService.queryAllGoods(id,chose);
        //System.out.println(goods);
        String goodsJson =null;
        if(goods!=null){
            goodsJson = "{\"status\":1,\"info\":"+gson.toJson(goods)+"}";
        }else{
            goodsJson = "{\"status\":0}";
        }
        response.getWriter().print(goodsJson);
    }



    /**
     * 更新商品信息
     * @param request goods
     * @param response 1为更新成功，0为更新失败
     */
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Goods goods = WebUtils.copyParamToBean(request.getParameterMap(),new Goods());
        Map<String, Integer> map = new HashMap<>(16);
        if(goodsService.updateGoods(goods)){
            if(goods.getNum()==0){
                if(goodsService.deleteGoods(goods.getId())){
                    map.put("status", 1);
                }else{
                    map.put("status", 0);
                }
            }else{
                map.put("status", 1);
            }
        }else{
            map.put("status", 0);
        }
        Gson gson = new Gson();
        String goodsJson = gson.toJson(map);
        response.getWriter().print(goodsJson);
    }

    /**
     * 通过商品的id查询该商品的数目
     * @param request id
     * @param response num
     */
    protected void num(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        System.out.println("in num...");
        Integer num = goodsService.queryGoodsNumById(id);
        System.out.println(num);
        String goodsJson=null;
        if(num!=null){
            goodsJson = "{\"status\":1,\"num\":"+num.toString()+"}";
        }else{
            goodsJson = "{\"status\":0}";
        }
        response.getWriter().print(goodsJson);
    }

    /**
     * 通过商品id查找商品
     * @param request id
     * @param response 商品对象及status:1表示成功获取对象，0表示失败
     */
    protected void queryGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Goods goods = goodsService.queryGoodsById(id);
        Gson gson = new Gson();
        String goodsJson =null;
        if(goods!=null){
            goodsJson = gson.toJson(goods);
            goodsJson ="{\"status\":1,\"info\":" + goodsJson+"}";
        }else {
            Map<String, Integer> map = new HashMap<>(16);
            map.put("status", 0);
            goodsJson = gson.toJson(map);
        }
        response.getWriter().print(goodsJson);
    }

    /**
     * 查询所有商品的总数量
     * @param request 无
     * @param response 商品数量 及status:1表示成功获取商品数量，0表示失败
     */
    protected void queryAllNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer num = goodsService.queryForGoodsNum();
        String goodsJson = num.toString();
        if(num!=null){
            goodsJson = "{\"status\":1,\"num\":"+goodsJson+"}";
        }else{
            goodsJson = "";
            goodsJson = "{\"status\":0}";
        }
        //System.out.println(goodsJson);
        response.getWriter().print(goodsJson);

    }

    /**
     * 查询最近的 num 条商品信息
     * @param request num 最近几条
     * @param response 商品列表
     */
    protected void queryLatest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int num = 5;
        List<Goods> goods = goodsService.queryLatestGoods(num);
        Gson gson = new Gson();
        String goodsJson = null;
        if(goods!=null){
            goodsJson = gson.toJson(goods);
            goodsJson = "{\"status\":1,\"info\":"+goodsJson+"}";
        }else{
            goodsJson = "{\"status\":0}";
        }
        response.getWriter().print(goodsJson);
    }
}
